package com.timmy.chi.ui.view;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.timmy.chi.R;
import com.timmy.chi.model.SimpleAnimatorListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

public class OneKeyDeleteEditView extends RelativeLayout {
    private Unbinder unbinder;
    private OnInputViewActionListener mOnInputViewActionListener;
    @BindView(R.id.inputEditText)
    EditText inputEditText;
    @BindView(R.id.cancelView)
    View cancelView;
    /** 默认提示内容 */
    private String mHintContent;

    public OneKeyDeleteEditView(Context context) {
        super(context);
        init();
    }

    public OneKeyDeleteEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public OneKeyDeleteEditView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.one_key_delete_edit_view, this);
        unbinder = ButterKnife.bind(this, this);
    }

    /**
     * 设置搜索提示
     */
    public void setHint(String hint) {
        if (null != inputEditText) {
            mHintContent = hint;
            inputEditText.setHint(hint);
        }
    }

    /**
     * 设置输入文本类型
     * @param inputType
     */
    public void setInputType(int inputType) {
        if (null != inputEditText) {
            inputEditText.setInputType(inputType);
        }
    }

    /**
     * 设置最大长度
     * @param maxLength
     */
    public void setMaxLength(int maxLength) {
        if (null != inputEditText) {
            inputEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        }
    }

    /**
     * 设置输入监听
     * @param textWatcher
     */
    public void setTextChangedListener(TextWatcher textWatcher) {
        if (null != inputEditText) {
            inputEditText.addTextChangedListener(textWatcher);
        }
    }

    /**
     * 获取输入的文本
     * @return
     */
    public String getText() {
        if (null != inputEditText) {
            return inputEditText.getText().toString();
        }
        return null;
    }

    /**
     * 设置文本
     * @param text
     */
    public void setText(String text) {
        if (null != inputEditText) {
            inputEditText.setText(text);
        }
    }

    /**
     * 设置光标所在位置
     * @param start
     * @param stop
     */
    public void setSelection(int start, int stop) {
        if (null != inputEditText) {
            inputEditText.setSelection(start, stop);
        }
    }

    /**
     * 设置错误提示
     * @param error
     */
    public void setError(String error) {
        if (null != inputEditText) {
            inputEditText.setError(error);
        }
    }

    @OnClick(R.id.cancelView)
    public void onClickByButterknife(View view) {
        switch (view.getId()) {
            case R.id.cancelView:
                inputEditText.setText("");
                if (!TextUtils.isEmpty(mHintContent)) inputEditText.setHint(mHintContent);
                if (null != mOnInputViewActionListener) mOnInputViewActionListener.onAction(OnInputViewActionListener.ACTION_CLEAR_INPUT);
                break;
        }
    }

    @OnTextChanged(value = R.id.inputEditText, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterTextChangedByButterKnife(Editable s) {
        String content = s.toString();
        if (TextUtils.isEmpty(content)) {
            if (cancelView.getVisibility() == View.VISIBLE) {
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.setDuration(500);
                animatorSet.playTogether(ObjectAnimator.ofFloat(cancelView, "scaleX", 1f, 0f), ObjectAnimator.ofFloat(cancelView, "scaleY", 1f, 0f));
                animatorSet.addListener(new SimpleAnimatorListener() {
                    @Override
                    public void onAnimationEnd(Animator animator) {
                        cancelView.setVisibility(View.INVISIBLE);
                        cancelView.setEnabled(false);
                        if (null != mOnInputViewActionListener) mOnInputViewActionListener.onAction(OnInputViewActionListener.ACTION_CLEAR_INPUT);
                        super.onAnimationEnd(animator);
                    }
                });
                animatorSet.start();
                cancelView.setEnabled(false);
            }
        } else {
            if (cancelView.getVisibility() == View.INVISIBLE) {
                cancelView.setEnabled(true);
                cancelView.setVisibility(View.VISIBLE);
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.setDuration(500);
                cancelView.setVisibility(View.VISIBLE);
                animatorSet.playTogether(ObjectAnimator.ofFloat(cancelView, "scaleX", 0f, 1f), ObjectAnimator.ofFloat(cancelView, "scaleY", 0f, 1f));
                animatorSet.addListener(new SimpleAnimatorListener() {
                    @Override
                    public void onAnimationEnd(Animator animator) {
                        if (null != cancelView) cancelView.setEnabled(true);
                        super.onAnimationEnd(animator);
                    }
                });
                animatorSet.start();
            }
        }
    }

    public void setInputViewActionListener(OnInputViewActionListener onInputViewActionListener) {
        mOnInputViewActionListener = onInputViewActionListener;
    }

    public interface OnInputViewActionListener {
        int ACTION_CLEAR_INPUT = 2;
        void onAction(int action);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        unbinder.unbind();
    }
}
